package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    EmpMapper empMapper;

    @Autowired
    EmpExprMapper empExprMapper;

    @Autowired
    EmpLogService empLogService;
    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        //1.获取count
        //Long total = empMapper.count();
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        //2.获取每个pagesize的数据
        //Integer start = (page - 1) * pageSize;
        List<Emp> rows = empMapper.list(empQueryParam);
        Page<Emp> p = (Page<Emp>) rows;
        return new PageResult<Emp>(p.getTotal(), p.getResult());

    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void save(Emp emp) {

        try {
            // 1. 插入员工数据
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());

            empMapper.saveEmp(emp);

            // 2. 插入工作经历
//        List<EmpExpr> empExprList = emp.getExprList();
//        if (empExprList != null && empExprList.size() > 0) {
//            for (EmpExpr empExpr : empExprList) {
//                empExpr.setEmpId(emp.getId());
//            }
//            empExprMapper.saveExpr(empExprList);
//        }
            Integer empId = emp.getId();
            List<EmpExpr> exprList = emp.getExprList();
            if(!CollectionUtils.isEmpty(exprList)){
                exprList.forEach(empExpr -> empExpr.setEmpId(empId));
                empExprMapper.saveExpr(exprList);
            }
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "新增员工信息："+ emp);
            empLogService.insertLog(empLog);
        }

    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {

        //1.删除emp表中员工数据
        empMapper.deleteEmpById(ids);

        //2.删除exper表中工作经历
        empExprMapper.deleteExprByEmpId(ids);
    }

    @Override
    public Emp getInfo(Integer id) {

        Emp emp = empMapper.getInfo(id);

        return emp;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void update(Emp emp) {
        //1. 更新emp数据
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);

        //2.更新expr数据
        //2.1删除对应工作经历
        empExprMapper.deleteExprByEmpId(Arrays.asList(emp.getId()));
        List<EmpExpr> empExprList = emp.getExprList();

        //2.2插入新的工作经历
        if(!CollectionUtils.isEmpty(empExprList)){
            empExprMapper.saveExpr(empExprList);
        }

    }

    @Override
    public LoginInfo login(Emp emp) {

        Emp empLogin = empMapper.login(emp);

        if(empLogin != null){
            //生成jwt令牌
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("id", empLogin.getId());
            dataMap.put("username", empLogin.getUsername());
            String jwts = JwtUtils.generateJwt(dataMap);

            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwts);
            return loginInfo;
        }
        return null;
    }


}
